const md5=require('md5');
const https=require('https');
const rq=require('request-promise');
const config=require('../config');
const logger=require('./logHelper').helper;

let wechat={
    wechatCode:(code)=>{
        return new Promise((resolve,reject)=>{
            let url='https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code';
            url=url.replace('APPID',config.appId).replace('SECRET',config.appSecret).replace('JSCODE',code);
            logger.writeInfo('wechatCode.url='+url);
            https.get(url,clientRes=>{
                let reqData='';
                clientRes.on('data',(data)=>{
                    reqData+=data;
                });
                clientRes.on('end',()=>{
                    logger.writeInfo('wechatCode.result='+reqData);
                    reqData=JSON.parse(reqData);
                    resolve(reqData);
                });
            })
        })
    }
};
module.exports=wechat;